﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using LitJson;

//[ExecuteInEditMode]
public abstract class PUEXHandle : MonoBehaviour {

	public enum EvnetType {
		ET_EMITTER_START,
		ET_EMITTER_END,
		ET_PARTICLE_EMISSION,
		ET_PARTICLE_EXPIRATION,
		ET_PARTICLE_COLLIDED,
		ET_NOTIFY_EVENT,
	};
	
	protected static int __spawnIndex = 0;
	
	#region - Editable property
	public string handleName = "";
	public PUEXHandle.EvnetType eventType;
	public string eventName = "";
	#endregion
	
	#region - Property
	[HideInInspector]
	protected bool _enabled;
	public bool isEnabled () {
		return _enabled;
	}
	public virtual void enable () {
		_enabled = true;
	}
	public virtual void disable () {
		_enabled = false;
	}
	
	[HideInInspector]
	public PUEXEmitter ownerEmitter;
	#endregion
	
	#region - Lifecycle
	public PUEXHandle () 
	{
		eventType = PUEXHandle.EvnetType.ET_EMITTER_START;
		
		_enabled = false;
		ownerEmitter = null;
	}
	
	public void Reset ()
	{
		// need emitter
		var emitter = gameObject.GetComponent<PUEXEmitter> ();
		if (emitter == null)
		{
			Debug.LogError("<PUEX> no emitter with event handle");
		}
		
		if (handleName.Length == 0) {
			string typeName = GetType().Name;
			typeName = typeName.Replace("PUEX", "");
			handleName = gameObject.name + " " + typeName + " " + ++__spawnIndex;
		}
	}
	
	public void OnEnable ()
	{
		if (Application.isPlaying) {
			enable ();
		}
	}
	
	public void OnDisable ()
	{
		if (Application.isPlaying) {
			disable ();
		}
	}
	#endregion
	
	#region - State
	public virtual void prepared(PUEXEmitter emitter)
	{
		this.ownerEmitter = emitter;
	}
	
	public virtual void start ()
	{
		enable ();
	}
	
	public virtual void pause ()
	{
	}
	
	public virtual void resume ()
	{
	}
	
	public virtual void stop ()
	{
		disable ();
	}
	
	public virtual void unPrepared()
	{
	}
	#endregion
	
	#region - Event
	public virtual void notifyEmitterStart ()
	{
	}
	
	public virtual void notifyEmitterEnd ()
	{
	}
	
	public virtual void notifyParticleEmission (PUEXParticle particle)
	{
	}
	
	public virtual void notifyParticleExpiration (PUEXParticle particle)
	{
	}
	
	public virtual void notifyParticleCollided (PUEXParticle particle)
	{
	}
	
	public virtual void notifyEvent (string eventName)
	{
	}
	#endregion
	
	#region - Param property
	public virtual void updateParams (PUEXParticleSystem system)
	{
	}
	#endregion
	
	#region - Serialization
	public virtual JsonData save (JsonData emitterData, JsonData systemData)
	{
		JsonData ret = new JsonData ();
		
		ret ["type"] = GetType ().Name;
		ret ["name"] = handleName;
		
		switch (eventType) {
		case EvnetType.ET_EMITTER_START: {
			ret ["event_type"] = "emitter_start";
			break;
		}
		case EvnetType.ET_EMITTER_END: {
			ret ["event_type"] = "emitter_end";
			break;
		}
		case EvnetType.ET_PARTICLE_EMISSION: {
			ret ["event_type"] = "particle_emission";
			break;
		}
		case EvnetType.ET_PARTICLE_EXPIRATION: {
			ret ["event_type"] = "particle_expiration";
			break;
		}
		case EvnetType.ET_PARTICLE_COLLIDED: {
			ret ["event_type"] = "particle_collided";
			break;
		}
		case EvnetType.ET_NOTIFY_EVENT: {
			ret ["event_type"] = "notify_event";
			ret ["event_name"] = eventName;
			break;
		}
		}
		
		return ret;
	}
	#endregion

}
